package com.hrbxlh.virtualization.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 读取属性文件
 *
 * @author cheer
 */
public class PropertiesUtil {

    private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);

    private PropertiesUtil() {
    }

    private static Map<String, Properties> propertiesMap = new HashMap<>();

    public static String getProperty(String propertiesFileName, String key) {
        Properties properties = propertiesMap.get(propertiesFileName);
        if (properties == null) {
            properties = new Properties();
            try (InputStreamReader inputStreamReader = new InputStreamReader(PropertiesUtil.class.getClassLoader()
                    .getResourceAsStream(propertiesFileName), "UTF-8")) {
                properties.load(inputStreamReader);
            } catch (IOException e) {
                logger.error("配置文件读取异常", e);
            }
            propertiesMap.put(propertiesFileName, properties);
        }

        String value = properties.getProperty(key.trim());
        if (StringUtils.isBlank(value)) {
            return null;
        }
        return value.trim();
    }
}
